#! /bin/make -f
# @file
# @brief Top makefile of board/project
# @author
# + 隐星魂 (Roy.Sun) <www.starsoul.tech>
# @copyright
# + (c) 2015 隐星魂 (Roy.Sun) <www.starsoul.tech>
# > Licensed under the Apache License, Version 2.0 (the "License");
# > you may not use this file except in compliance with the License.
# > You may obtain a copy of the License at
# >
# >         http://www.apache.org/licenses/LICENSE-2.0
# >
# > Unless required by applicable law or agreed to in writing, software
# > distributed under the License is distributed on an "AS IS" BASIS,
# > WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# > See the License for the specific language governing permissions and
# > limitations under the License.
#

WKSPC ?= wkspc
TARGET ?= XuanWuOS

all: $(WKSPC) cfg dbgcfg
	$(MAKE) -C $(WKSPC) TARGET=$(TARGET)

$(WKSPC):
	@mkdir -p $@

cfg: $(WKSPC) $(WKSPC)/XuanWuOS.cfg

$(WKSPC)/XuanWuOS.cfg: cfg/XuanWuOS.h cfg/arch.h cfg/cpu.h cfg/soc.h \
 cfg/perpheral.h cfg/board.h cfg/xwos.h cfg/xwmd.h cfg/xwem.h
	@../../xwbs/util/el/mkcfg.el --cfgdir=cfg --topdir=../.. --wkspc=$(WKSPC) -- $(@F)

dbgcfg: cfg/dbg/debug.gdb cfg/dbg/program.gdb

%.gdb: %.gdb.template
	@sed "s/##DIR##/$(subst /,\\/,$(shell pwd)/$(WKSPC))/g" $< > $@
	@chmod +x $@

jlink:
	@openocd -f cfg/dbg/openocd/jlink-swd.cfg

c clean: cfg
	$(MAKE) -C $(WKSPC) TARGET=$(TARGET) NODEP=y clean

d distclean: cfg
	@rm -rf $(WKSPC)
	@rm -rf cfg/dbg/*.gdb

.DEFAULT:
	@$(MAKE) cfg
	$(MAKE) -C $(WKSPC) TARGET=$(TARGET) $@

.PHONY : all cfg dbgcfg c clean d distclean
